//
//  WBVisitorView.swift
//  Weibo-Swift
//
//  Created by law on 2018/8/27.
//  Copyright © 2018年 JKing. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册",
                                                         fontSize: 16,
                                                         normalColor: UIColor.orange,
                                                         highlightedColor: UIColor.black,
                                                         backgroundImageName: "common_button_white_disable")
    /// 登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录",
                                                      fontSize: 16,
                                                      normalColor: UIColor.darkGray,
                                                      highlightedColor: UIColor.black,
                                                      backgroundImageName: "common_button_white_disable")
    /// 设置访客视图数据
    var visitorInfo: [String: String]? {
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            tipLabel.text = message
            
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconView.image = UIImage(named: imageName)
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 圆圈动画
    private func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        anim.isRemovedOnCompletion = false
        
        iconView.layer.add(anim, forKey: nil)
        
    }
    
    // MARK: - 私有控件
    /// 圆圈⭕️
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子
    private lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 提示标签
    private lazy var tipLabel: UILabel = UILabel(text: "关注一些人，回这里看看有什么惊喜",
                                                 fontSize: 14,
                                                 color: UIColor.darkGray)
}


extension WBVisitorView {
    
    /// 搭建UI
    func setupUI() {
        backgroundColor = UIColor.init(hex: 0xEDEDED)
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        
        maskIconView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        
        houseIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(iconView.snp.bottom).offset(20)
            make.width.equalTo(256)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(10)
            make.left.equalTo(tipLabel)
            make.size.equalTo(CGSize(width: 100, height: 40))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(registerButton)
            make.right.equalTo(tipLabel)
            make.size.equalTo(CGSize(width: 100, height: 40))
        }
    }
    
}
